const path = require('path');

const { CleanWebpackPlugin } = require('clean-webpack-plugin');

const TerserPlugin = require('terser-webpack-plugin');

const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const HtmlWebpackPlugin = require('html-webpack-plugin');

//webpack全局变量
const webpack = require('webpack');


// ————————test
const getHtmlConfig = function (name) {
    return {
        template: './src/view/' + name + '.html',
        filename: 'view/' + name + '.html',
        inject: true,
        hash: true,
        chunks: ['common', name],
        minify: {
            collapseWhitespace: false
        }
    }
}

var config = {
    entry: {
        'common': ['./src/page/common/index.js'],
        'index': ['./src/page/index/index.js'],
        'catalog-main': ['./src/page/catalog-main/index.js'],
        'normal': ['./src/page/normal/index.js'],
        'product_details': ['./src/page/product_details/index.js'],
        'product_summary': ['./src/page/product_summary/index.js'],
        'products': ['./src/page/products/index.js'],
        'register': ['./src/page/register/index.js'],
        'special_offer': ['./src/page/special_offer/index.js'],
        'tac': ['./src/page/tac/index.js'],
        'faq': ['./src/page/faq/index.js'],
        'login': ['./src/page/login/index.js'],
        'contact': ['./src/page/contact/index.js'],
        'compair': ['./src/page/compair/index.js'],
        'forgetpass': ['./src/page/forgetpass/index.js'],
        'legal_notice': ['./src/page/legal_notice/index.js'],
        'orders_all': ['./src/page/orders_all/index.js'],
        'order_detail': ['./src/page/order_detail/index.js'],
        'receiving_all': ['./src/page/receiving_all/index.js'],
        'receiving_detail': ['./src/page/receiving_detail/index.js'],
        'favourites': ['./src/page/favourites/index.js'],
        'mystore': ['./src/page/mystore/index.js'],
        'mystore_detail': ['./src/page/mystore_detail/index.js'],
        'mystore_order_detail': ['./src/page/mystore_order_detail/index.js'],
        'records': ['./src/page/records/index.js'],
        'profile': ['./src/page/profile/index.js']
    },
    output: {
        filename: 'js/[name].js',
        path: path.resolve(__dirname, 'dist'),
        globalObject: 'this'
    },
    devServer: {
        port: 8888,
        //新版webpack-cli为static
        static: path.resolve(__dirname, 'dist'),

    },
    stats: {
        warnings: false
    },
    performance: {
        hints: false
    },

    module: {
        rules: [
            {
                test: /\.css$/,
                // include: [
                //     path.resolve(__dirname, "not_exist_path")
                // ],    
                use: [
                    // {
                    //     loader: MiniCssExtractPlugin.loader,
                    //     options: {
                    //         publicPath: '../'
                    //     }
                    // },
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.less$/,
                // include: [
                //     path.resolve(__dirname, "not_exist_path")
                // ],    
                use: [
                    // {
                    //     loader: MiniCssExtractPlugin.loader,
                    //     options: {
                    //         publicPath: '../'
                    //     }
                    // },
                    'style-loader',
                    'css-loader',
                    'less-loader'
                ]
            },
            {
                test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
                use: {
                    loader: "file-loader"
                }
            },
            {
                test: /\.(woff|woff2)$/,
                use: {
                    loader: "url-loader",
                    options: {
                        prefix: "font",
                        limit: 5000,
                        outputPath: "fonts",
                        esModule: false
                    }
                }
            },
            {
                test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
                use: {
                    loader: "url-loader",
                    options: {
                        limit: 10000,
                        mimetype: "application/octet-stream",
                        outputPath: 'fonts',
                        esModule: false
                    }
                }
            },
            {
                test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            limit: 10000,
                            mimetype: "image/svg+xml",
                            outputPath: "svg",
                            esModule: false
                        }
                    }
                ]
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            name: '[name].[ext]',
                            outputPath: 'image',
                            limit: 10,
                            esModule: false
                        }
                    }
                ],
                type: 'javascript/auto'
            },
            // {
            //     test: /\.htm$/,
            //     use: {
            //         loader: 'html-loader',
            //         options: {
            //             esModule: false,
            //             minimize: false
            //         }
            //     }
            // }
            {
                test: /\.html$/,
                use: {
                    loader: 'html-withimg-loader',
                    // options: {
                    //     esModule: false,
                    //     minimize: false
                    // }
                }
            }
        ]
    },
    plugins: [
        // new CleanWebpackPlugin(),
        // new webpack.ProvidePlugin({
        //     $: "jquery",
        //     jQuery: "jquery"
        // }),   
        new MiniCssExtractPlugin({
            filename: 'css/[name].css'
        }),
        new HtmlWebpackPlugin(getHtmlConfig('index')),
        new HtmlWebpackPlugin(getHtmlConfig('catalog-main')),
        new HtmlWebpackPlugin(getHtmlConfig('login')),
        new HtmlWebpackPlugin(getHtmlConfig('contact')),
        new HtmlWebpackPlugin(getHtmlConfig('compair')),
        new HtmlWebpackPlugin(getHtmlConfig('faq')),
        new HtmlWebpackPlugin(getHtmlConfig('forgetpass')),
        new HtmlWebpackPlugin(getHtmlConfig('legal_notice')),
        new HtmlWebpackPlugin(getHtmlConfig('normal')),
        new HtmlWebpackPlugin(getHtmlConfig('product_details')),
        new HtmlWebpackPlugin(getHtmlConfig('product_summary')),
        new HtmlWebpackPlugin(getHtmlConfig('products')),
        new HtmlWebpackPlugin(getHtmlConfig('register')),
        new HtmlWebpackPlugin(getHtmlConfig('special_offer')),
        new HtmlWebpackPlugin(getHtmlConfig('tac')),
        new HtmlWebpackPlugin(getHtmlConfig('orders_all')),
        new HtmlWebpackPlugin(getHtmlConfig('order_detail')),
        new HtmlWebpackPlugin(getHtmlConfig('receiving_detail')),
        new HtmlWebpackPlugin(getHtmlConfig('receiving_all')),
        new HtmlWebpackPlugin(getHtmlConfig('favourites')),
        new HtmlWebpackPlugin(getHtmlConfig('mystore')),
        new HtmlWebpackPlugin(getHtmlConfig('mystore_detail')),
        new HtmlWebpackPlugin(getHtmlConfig('mystore_order_detail')),
        new HtmlWebpackPlugin(getHtmlConfig('records')),
        new HtmlWebpackPlugin(getHtmlConfig('profile')),
        new webpack.HotModuleReplacementPlugin() //热替换
    ],
    // externals:{
    //     'jquery':'window.jQuery'
    // },
    optimization: {
        minimize: false,
        minimizer: [
            new TerserPlugin({
                extractComments: false,
            }),
        ],
        splitChunks: {
            cacheGroups: {
                commons: {
                    name: 'utils',
                    chunks: 'all',
                    minChunks: 2,
                    minSize: 0
                },
            }
        }
    },
    resolve: {
        alias: {    //起别名
            util: __dirname + '/src/util',
            page: __dirname + '/src/page',
            images: __dirname + '/src/images',
            service: __dirname + '/src/service',
            node_modules: __dirname + '/node_modules'
        }
    }
};

module.exports = config;